package com.enjoyor.health.HGPT.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.enjoyor.health.HGPT.core.dto.ReservationNumDto;
import com.enjoyor.health.HGPT.core.dto.ScheduleInfoDto;
import com.enjoyor.health.HGPT.core.entity.ReservationInfo;
import com.enjoyor.health.HGPT.core.mapper.ReservationInfoMapper;
import com.enjoyor.health.HGPT.core.service.IReservationInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author enjoyor
 * @since 2023-10-18
 */
@Service
public class ReservationInfoServiceImpl extends ServiceImpl<ReservationInfoMapper, ReservationInfo> implements IReservationInfoService {

    @Autowired
    private ReservationInfoMapper reservationInfoMapper;

    public List<ScheduleInfoDto> getReservationInfoByOpenId(String openId) {
        Map<String, Object> map = new HashMap<>();
        map.put("open_id", openId);
        List<ScheduleInfoDto> result =  reservationInfoMapper.getReservationInfoByOpenId(map);
        Map<String, ScheduleInfoDto> dtoMap = Maps.newLinkedHashMap();
        for (ScheduleInfoDto scheduleInfoDto : result) {
            if ("1".equals(scheduleInfoDto.getFlag()) && StringUtils.isBlank(scheduleInfoDto.getReportVersion())){
                //这种情况是体检了，但是报告没出，应该是另外一种类型。
                scheduleInfoDto.setFlag("3");
            }
            dtoMap.put(scheduleInfoDto.getReportVersion() + "_" +
                    scheduleInfoDto.getReservationNumber() + "_" +
                    scheduleInfoDto.getPackageType(), scheduleInfoDto);
        }
        return new ArrayList<>(dtoMap.values());
    }

    @Override
    public List<ReservationNumDto> getReservationNumBetweenDate(String startDate, String endDate) {
        Map<String, Object> map = new HashMap<>();
        map.put("start_date", startDate);
        map.put("end_date", endDate);
        return baseMapper.getReservationNumBetweenDate(map);
    }

    @Override
    public List<ReservationNumDto> getReservationNumBetweenDateV2(String startDate, String endDate, List<String> packageIds) {
        Map<String, Object> map = new HashMap<>();
        map.put("start_date", startDate);
        map.put("end_date", endDate);
        map.put("reservation_package_ids", packageIds);
        return baseMapper.getReservationNumBetweenDateV2(map);
    }

    @Override
    public List<ReservationNumDto> getReservationNumByDate(String date) {
        Map<String, Object> map = new HashMap<>();
        map.put("date", date);
        return baseMapper.getReservationNumByDate(map);
    }

    @Override
    public List<ReservationNumDto> getReservationNumByDateV2(String date, List<String> packageIds) {
        Map<String, Object> map = new HashMap<>();
        map.put("date", date);
        map.put("reservation_package_ids", packageIds);
        return baseMapper.getReservationNumByDateV2(map);
    }

    @Override
    public Page<Map<String, Object>> getReservationInfoWeb(LocalDate date, String pkgType,
                                                           String startDate,
                                                           String endData,
                                                           String period,
                                                           String status, int page ,int size) {
        Map<String, Object> map = Maps.newHashMap();
        map.put("date", date.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        map.put("date_pure", date.format(DateTimeFormatter.ofPattern("yyyyMMdd")));
        map.put("pkg_type", pkgType);
        map.put("start_date", startDate);
        map.put("end_date", endData);
        map.put("period", period);
        map.put("check_status", status);
        Page<Map<String, Object>> p = new Page<>(page, size);
        p.addOrder(OrderItem.desc("reservation_date"), OrderItem.asc("reservation_period"), OrderItem.asc("reservation_number"));
        return baseMapper.getReservationInfoWebPage(p, map);
    }

    @Override
    public void updateReservationInfoUpdateTime(String reservationNumber) {
        LambdaUpdateWrapper<ReservationInfo> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(ReservationInfo::getReservationNumber, reservationNumber)
                .set(ReservationInfo::getPresentTime, LocalDateTime.now());
        baseMapper.update(null, lambdaUpdateWrapper);
    }
}
